from django.contrib.contenttypes.models import ContentType
from django.utils.translation import ugettext_lazy as _
from django.contrib.gis.geos import geometries
from django.contrib.gis.db import models

from duetopia.opensearch import OpenSearch
from duetopia import rdf

__all__ = ["Series", "DataSet"]

class Series(models.Model):
	__module__ = "duetopia.register.models"
	class Meta:
		ordering = ("-start",)
		verbose_name = _("series")
		verbose_name_plural = _("series")
	class RDF(rdf.Schema):
		name = "annotation"
		properties = (
			("title", rdf.dc.title),
			("start", rdf.ical.dtstart),
			("end", rdf.ical.dtend),
			("extents", rdf.dct.spatial)
			)
	class Search(OpenSearch):
		ShortName = _("Data Series")
		Description = _("A Series is a collection of data sets. " +
				"Search through the series by keyword tag " +
				"or by free text search in the title. " +
				"Narrow the search by a date range or " +
				"by selecting a geographic area of interest " +
				"on the map using the Control key and the mouse.")
		namespaces = {
			"geo" : "http://a9.com/-/opensearch/extensions/geo/1.0/",
			"time" : "http://a9.com/-/opensearch/extensions/time/1.0/",
			}
		search_terms = ({ "name" : "keyword",
				  "type" : "rdf",
				  "uri" : rdf.dc.subject.uri,
				  "query" : "searchTerms?" },
				{ "name" : "title",
				  "type" : "django",
				  "filter" : "title__icontains",
				  "query" : "searchTerms?" },
				{ "name" : "start",
				  "type" : "django",
				  "filter" : "start__gte",
				  "query" : "time:Start?" },
				{ "name" : "end",
				  "type" : "django",
				  "filter" : "end__lt",
				  "query" : "time:End?" },
				{ "name" : "geometry",
				  "type" : "geodjango",
				  "filter" : "extents__intersects",
				  "query" : "geo:box" },
				)
		display_fields = ["title", "start", "end"]
		geometry_field = "extents"
	objects = rdf.Manager()
	uri = models.URLField(verbose_name=_("URI"), unique=True)
	created = models.DateTimeField(verbose_name=_("created"), auto_now_add=True)
	title = models.CharField(verbose_name=_("title"), max_length=1024)
	start = models.DateTimeField(verbose_name=_("start"), null=True)
	end = models.DateTimeField(verbose_name=_("end"), null=True)
	extents = models.PolygonField(verbose_name=_("extents"), null=True)
	def __str__(self):
		return str(self.title)
	def __unicode__(self):
		return unicode(self.title)
	def get_absolute_url(self):
		return "/duetopia/%s/%s/%s/" % \
		    (self._meta.app_label, self._meta.module_name, self.pk)
		
class DataSet(models.Model):
	__module__ = "duetopia.register.models"
	class Meta:
		ordering = ("-start",)
		verbose_name = _("dataset")
		verbose_name_plural = _("datasets")
	class Search(OpenSearch):
		ShortName = _("Data Sets")
		Description = _("Search for individual data sets. "
				"This section is a generic or common search. "
				"For more granular or data type specific searching "
				"you may wish to use one of the other tabs. "
				"Double-click on a row in the results to see complete "
				"meta-data for the selected data set. "
				"Mouse over a row in the results to see that result's "
				"location on the map.")
		namespaces = {
			"geo" : "http://a9.com/-/opensearch/extensions/geo/1.0/",
			"time" : "http://a9.com/-/opensearch/extensions/time/1.0/",
			}
		search_terms = ({ "name" : "keyword",
				  "type" : "rdf",
				  "uri" : rdf.dc.subject.uri,
				  "query" : "searchTerms?" },
				{ "name" : "title",
				  "type" : "django",
				  "filter" : "title__icontains",
				  "query" : "searchTerms?" },
				{ "name" : "start",
				  "type" : "django",
				  "filter" : "start__gte",
				  "query" : "time:Start?" },
				{ "name" : "end",
				  "type" : "django",
				  "filter" : "end__lt",
				  "query" : "time:End?" },
				{ "name" : "geometry",
				  "type" : "geodjango",
				  "filter" : "extents__intersects",
				  "query" : "geo:polygon" },
				)
		display_fields = ("title", "start", "end")
		geometry_field = "extents"
	class RDF(rdf.Schema):
		name = "annotation"
		properties = (
			("series", rdf.parser.Resource(rdf.dclite4g.series, Series)),
			("title", rdf.dc.title),
			("start", rdf.ical.dtstart),
			("end", rdf.ical.dtend),
			("extents", rdf.dct.spatial)
			)
	objects = rdf.Manager()
	uri = models.URLField(verbose_name=_("URI"), unique=True)
	created = models.DateTimeField(verbose_name=_("created"), auto_now_add=True)
	series = models.ForeignKey(Series, verbose_name=_("series"))
	title = models.CharField(verbose_name=_("title"), max_length=1024)
	content_type = models.ForeignKey(ContentType, null=True, editable=False)
	start = models.DateTimeField(verbose_name=_("start"), null=True)
	end = models.DateTimeField(verbose_name=_("end"), null=True)
	extents = models.PolygonField(verbose_name=_("extents"), null=True)

	def get_absolute_url(self):
		return "/duetopia/%s/%s/%s/" % \
		    (self._meta.app_label, self._meta.module_name, self.pk)

	def save(self):
		if not self.title:
			self.title = self.series.title
		if not self.extents:
			self.extents = self.series.extents
		super(DataSet, self).save()
